  layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form','treetable'], function(){
      var form = layui.form;
      var table = layui.table;

      var Page = {
          tableId: 'sys-MEStask-table',
          url: CXL.ctxPath + '/sys/addTask/taskDistribute/getDetailTaskInfo',
          addPageUrl:CXL.ctxPath + '/sys/addTask/openPage',
          editPageUrl:CXL.ctxPath + '/sys/addTask/openEditPage',
          initColumn:function(){
              return [[
                  {type : 'numbers',title : '序号',width:'3%'},
                  {field:'idKey',title:'idKey',hide:true,width:'1%'},
                  {field:'taskId',title:'任务号',width:'6%'},
                  {field:'materialName',title:'物料名称',width:'12%'},
                  {field:'materialNo',title:'物料编码',width:'12%'},
                  {field:'status',title:'任务状态',width:'12%',templet:"#online-status"},
                  {field:'targetWeight',title:'目标重量',width:'12%'},
                  {field:'actualWeight',title:'实际重量',width:'12%'},
                  {title:'操作',width:'15%',toolbar: '#sys-task-tool'}
              ]];
          },
      };





      //增加物料
      // $("#btnAdd").click(function (){
      //
      //     var tankCarNo = $('#tankCarNo option:selected').val();
      //     if(tankCarNo == null || tankCarNo == ''){
      //         CXL.warn('请选择对应物料信息的槽车');
      //         return;
      //     }

      //     Page.openAddPage(taskId,null,"I",tankCarNo);
      // });


      //查询主任务接口
      $("#btnMESDistribute").click(function (){

          var taskId = $('#taskId').val();

          if(taskId == null || taskId == ''){
              CXL.warn('请输入任务号');
              return;
          }


          Page.getTaskInfo(taskId);
      });

      //增加物料任务
      $().click(function (){
          Page.openAddPage(taskId,null,"I",tankCarNo);
      });


      //编辑任务类型
      table.on('tool(' + Page.tableId + ')', function(obj){
          //获得当前行数据
          var data = obj.data;

          var taskType = $('#taskType option:selected').val();
          var idKey = data.idKey;
          var taskId = data.taskId;
          //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
          var layEvent = obj.event;
          var tankCarNo = $('#tankCarNo option:selected').val();

          //编辑操作
          if(layEvent === 'edit'){
              Page.openEdit(idKey);
          }
          if(layEvent === 'delDetailTask'){
              Page.del(idKey,taskId,tankCarNo);
          }


          if(layEvent == 'addMaterial'){
              Page.openAddPage(taskId,null,"I",tankCarNo);
          }
          if(layEvent == 'doTask'){
              var spanNo = $('#spanNo option:selected').val();
              var carNo = $('#carNo option:selected').val();

              if(taskType == null || taskType == ''){
                  CXL.warn('请选择任务操作类型');
                  return;
              }
              if(spanNo == null || spanNo == ''){
                  CXL.warn('请选择工作跨');
                  return;
              }

              if(carNo == null || carNo == ''){
                  CXL.warn('请选择天车');
                  return;
              }


              Page.distribute(taskType,tankCarNo,taskId,0,spanNo,carNo);

          }
      });


      Page.getTaskInfo = function (taskId){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/getTaskInfo',
              data:{taskId:taskId},
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                      table.render({
                          elem:'#' + Page.tableId,
                          data:res.obj,
                          height:'full',
                          page:false,
                          autoSort:false,
                          cols:Page.initColumn()
                      });
                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });


      };

      Page.del = function (idKey,taskId){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/del',
              data:{idKey:idKey,taskId:taskId},
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                      Page.getTaskInfo(taskId);
                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });

      };

      Page.distribute = function (taskType,tankCarNo,taskId,materialNum,spanNo,carNo){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/confirm',
              data:{tankCarNo:tankCarNo,oprType:taskType,taskId:taskId,materialNum:materialNum,spanNo:spanNo,carNo:carNo},
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });
          window.location.reload();
      };

      Page.delPriTask = function (){

      }

      Page.openEdit = function (idKey){
          layer.open({
              type : 2,
              title : '编辑物料',
              shadeClose : true,
              shade : 0.8,
              area: ['600px', '600px'],
              content : Page.editPageUrl + "?idKey=" +idKey ,
              btn : [ '确定', '取消' ],
              yes : function(index, layero) {
                  //点击确认触发 iframe 内容中的按钮提交
                  var submit = layero.find('iframe').contents().find("#form-btn-save");
                  submit.click();

              },
              //层销毁后触发的回调
              end:function (){
                  var taskId = $('#taskId').val();
                  if(taskId != null && taskId != ''){
                      Page.getTaskInfo(taskId);
                  }

              }

          });
      };


      Page.openAddPage = function (taskId,idKey,oprType,tankCarNo){
          //任务号id  子任务主键id   操作类型
          if(taskId == null){
              taskId = "";
          };
          if(idKey == null){
              idKey = "";
          };
          if(oprType == null){
              oprType = "";
          };

          layer.open({
              type : 2,
              title : '增加物料',
              shadeClose : true,
              shade : 0.8,
              area: ['600px', '600px'],
              content : Page.addPageUrl + "?taskId=" + taskId + "&idKey=" + idKey + "&oprType="+oprType + "&tankCarNo=" + tankCarNo,
              btn : [ '确定', '取消' ],
              yes : function(index, layero) {
                  //点击确认触发 iframe 内容中的按钮提交
                  var submit = layero.find('iframe').contents().find("#form-btn-save");
                  submit.click();

              },
              //层销毁后触发的回调
              end:function (){
                  var taskId = $('#taskId').val();
                  if(taskId != null && taskId != ''){
                      Page.getTaskInfo(taskId);
                  }

              }
          });

      };




  })